package com.nervenets.web.controller;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.nervenets.aspect.RedisLocker;
import com.nervenets.utils.JodaUtils;
import com.nervenets.web.hibernate.dao.UserDao;
import com.nervenets.web.redis.GlobalService;
import com.nervenets.web.redis.GlobalVariablesService;
import com.nervenets.web.redis.utils.RedisSyncLocker;
import com.nervenets.web.service.MailService;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@EnableScheduling
public class SchedulerTask {
    @Resource
    private UserDao userDao;
    @Resource
    private GlobalService globalService;
    @Resource
    private GlobalVariablesService globalVariablesService;
    @Resource
    private RedisSyncLocker redisSyncLocker;
    @Resource
    private MailService mailService;
    @Resource
    private WxPayService wxPayService;

    /**
     * 每23秒钟执行
     */
    @Scheduled(cron = "*/23 * * * * *")
    @RedisLocker(lockKey = "SchedulerTask.every5SecondsExecute", holdTime = 20)
    public void every23SecondsExecute() throws Exception {
        final int start = JodaUtils.getTimestamp();
        System.out.print("SchedulerTask every23SecondsExecute start " + JodaUtils.parse(start * 1000L));

        System.out.println(", execute finished, token " + (JodaUtils.getTimestamp() - start) + "s");
    }


    /**
     * 每小时执行
     */
    @Scheduled(cron = "30 0 * * * *")
    @RedisLocker(lockKey = "SchedulerTask.everyHourExecute", holdTime = 3000)
    public void everyHourExecute() throws Exception {
        System.out.println("SchedulerTask.everyHourExecute execute...");
        int startOfLastHour = JodaUtils.getStartOfLastHour();

        //每小时更新一次微信OAuthToken
        //final Weixin weixin = Global.getWeixin();
        //weixin.reLogin();
        //globalVariablesService.setWeChatAuthToken(weixin.getOAuthToken());
    }

    /**
     * 每天执行
     *
     * @throws Exception
     */
    @RedisLocker(lockKey = "SchedulerTask.everyDayExecute", holdTime = 7200)
    @Scheduled(cron = "0 0 1 ? * *")
    public void everyDayExecute() throws Exception {
        System.out.println("SchedulerTask.everyDayExecute execute...");
        final int startOfYesterday = JodaUtils.getStartOfYesterday();

        //生成明天的基础统计数据
        final int mtStart = JodaUtils.getEndOfToday() + 1;
    }

    /**
     * 每周执行
     *
     * @throws Exception
     */
    @RedisLocker(lockKey = "SchedulerTask.everyWeekExecute", holdTime = 7200)
    @Scheduled(cron = "0 0 1 ? * MON")
    public void everyWeekExecute() throws Exception {
        System.out.println("SchedulerTask.everyWeekExecute execute...");
    }


    /**
     * 每月执行一次
     */
    @RedisLocker(lockKey = "SchedulerTask.everyMonthExecute", holdTime = 7200)
    @Scheduled(cron = "0 0 1 1 * *")
    public void everyMonthExecute() {
        System.out.println("SchedulerTask.everyMonthExecute execute...");

    }


    protected WxPayRefundResult wxRefund(String tradeNo, int amount, String desc) throws WxPayException {
        WxPayRefundRequest refundRequest = WxPayRefundRequest.newBuilder()
                .outRefundNo(tradeNo)
                .outTradeNo(tradeNo)
                .refundFee(amount)
                .totalFee(amount)
                .refundDesc(desc)
                .build();
        return wxPayService.refund(refundRequest);
    }
}
